<?php

namespace Home\Widget;
use Think\Controller;

/**
 * 微信文字处理
 */
class TextWidget extends Controller {

    public function index($data){
    
    	$artid= C('WXARTID');
    	$mapart['id']=array('in',$artid);
    	$artarr= M('article')->where($mapart)->select();
    	$art=array();
        foreach ($artarr as $key =>$vo){
       	$art[$key]=array($vo['title'],'',getImgs($vo['description'],1),(is_ssl()?'https://':'http://').$_SERVER['HTTP_HOST'].'/'.C('WEB_DIR').'/'.ZSU('/artc/'.$vo['id'],'Index/artc',array('id'=>$vo['id'])));
      
       	
       	
       } 
    	$result = array(
            'MsgType' => 'text',
            'Content' => "如果您不是留言的话……\n\r\n您可以发送“help”或者“？”获取使用帮助。\n\r\n如果您确定要留言，您放心，您的留言我已经收到，我会尽快回复你的。\n\r\n谢谢您的支持！^_^"
        );
    	$wx = new \Common\Weixin\Weixin();
    	if ( $data['Content'] == 'help'||$data['Content'] == '？'||$data['Content'] == '?') {
    		$result = array(
    				'MsgType' => 'text',
    				'Content' => "发送“推荐”可发送当日精选博客\n\r\n发送“搜索”可搜索博客内容\n\r\n发送“退出搜索”可退出搜索\n\r\n",
    	
    		);
    	}
    	if ( $data['Content'] == '推荐') {
    		$result = array(
    				'MsgType' => 'news',
    				'Content' => $art,
    				
    		);
    	}
        if ( $data['Content'] == '谢谢' ) {
        	$wx->setValue('do',null);
            $result = array(
                'MsgType' => 'text',
                'Content' => '不用谢，这是我们应该做的！感谢您的支持！'
            );
            return $wx->toWeixin($result);
        }
        if ( $data['Content'] == '退出搜索' ) {
        	$wx->setValue('do',null);
        	$result = array(
        			'MsgType' => 'text',
        			'Content' => '已经退出搜索模式！'
        	);
        	return $wx->toWeixin($result);
        }
        if ( $data['Content'] == '搜索' ) {
        	$wx->setValue('do', 'search');
        	$result = array(
        			'MsgType' => 'text',
        			'Content' => '请输入关键词（只允许一个关键词）'
        	);
        	return $wx->toWeixin($result);
        }
        if ( $data['Content'] == '快递' ) {
        	$wx->setValue('do', 'express');
        	$result = array(
                'MsgType' => 'text',
                'Content' => '请以 快递公司名称!快递单号 的格式输入您需要查询的快递（如：汇通!210541983361）'
            );
            return $wx->toWeixin($result);
        }
        if ( $data['Content'] == '翻译' ) {
        	$wx->setValue('do', 'translate');
        	$result = array(
                'MsgType' => 'text',
                'Content' => '请输入要翻译的词语或短语（中英文皆可）'
            );
            return $wx->toWeixin($result);
        }
        if ( $data['Content'] == '天气' ) {
        	$result = $wx->weather();
            return $wx->toWeixin($result);
        }
        
        /* 以下为API函数调用处理 */
        $lastdo = $wx->getValue('do');
        if ( $lastdo == 'express' ) {
        	$result = $wx->express($data['Content']);
        }
        if ( $lastdo == 'search' ) {
        	
        	$map['title']=array('like','%'.$data['Content'].'%');
        	$map['status']=1;
        	$sartarr=M('article')->where($map)->select();
        	$sart=array();
        	foreach ($sartarr as $key =>$vo){
        		$sart[$key]=array($vo['title'],'',getImgs($vo['description'],1),(is_ssl()?'https://':'http://').$_SERVER['HTTP_HOST'].'/'.C('WEB_DIR').'/'.ZSU('/artc/'.$vo['id'],'Index/artc',array('id'=>$vo['id'])));
        	
        	
        	
        	}
        	
        	if(M('article')->where($map)->count()>0){
        		$result = array(
        				'MsgType' => 'news',
        				'Content' => $sart,
        		
        		);
        		
        	}else{
        		$result = array(
        				'MsgType' => 'text',
        				'Content' => '暂无相关内容！',
        		
        		);
        	}
        	
        	
        }
        if ( $lastdo == 'translate' ) {
        	$result = $wx->translate($data['Content']);
        }

        return $wx->toWeixin($result);
    }


}